import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect:'/home/index'
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    children:[
      {
        path: 'index',
        name: 'index',
        component: () => import('../views/Index.vue'),
      },
      {
        path: 'hospital-list',
        name: 'hospital-list',
        component: () => import('../views/hospital/HospitalList.vue'),
      },{
        path: 'hospital-update/:id',
        name: 'hospital-update',
        component: () => import('../views/hospital/HospitalUpdate.vue'),
      },{
        path: 'hospital-add',
        name: 'hospital-add',
        component: () => import('../views/hospital/HospitalAdd.vue'),
      },{
        path: 'doctor-list',
        name: 'doctor-list',
        component: () => import('../views/doctor/DoctorList.vue'),
      },{
        path: 'drug-list',
        name: 'drug-list',
        component: () => import('../views/durg/drug.vue'),
      },{
        path: 'new-list',
        name: 'new-list',
        component: () => import('../views/new/NewList.vue'),
      },{
        path: 'package-list',
        name: 'package-list',
        component: () => import('../views/package/Package.vue'),
      },
    ]
  },
  {
    path: '/user/login',
    name: 'user/login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/user/Login.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

import store from '@/store'

// 添加守卫
router.beforeEach((to,from,next) =>{
  if(to.path == '/user/login' || store.state.user){
    next()  // 判断下一步
  }else {
    router.push('/user/login')
  }
  // console.log(to),
  // next() // 继续下一步
})

export default router
